/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package healthcare.coursework.service;

import healthcare.coursework.model.Patient;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless(name="PatientService")
//@WebService
public class PatientServiceImpl implements PatientService {

    @PersistenceContext
    private EntityManager em;

    //@WebMethod
    public Patient findPatient(Long patientId) {
        Query q = em.createQuery("SELECT p FROM Patient p WHERE p.isDeleted ='FALSE' AND p.patientId=?1");
        q.setParameter(1, patientId);
        return (Patient) q.getSingleResult();
    }

     //@WebMethod
    public List<Patient> findPatientsByFullName(String patientName) {
        Query q = em.createQuery("SELECT p FROM Patient p WHERE p.isDeleted ='FALSE' AND p.fullName like ?1");
        q.setParameter(1, "%" +patientName +"%");
        return q.getResultList();
    }

    /**
     * Find all patients
     * @return
     */
    //@WebMethod
    public List<Patient> findPatients() {
        Query q = em.createQuery("SELECT p FROM Patient p WHERE p.isDeleted ='FALSE'");
        return q.getResultList();
    }

    /**
     * Return list of patients by tracking
     * @param trackCode
     * @return
     */
    //@WebMethod
    public Patient findPatientByTrackingCode(String trackCode) {
        Query q = em.createQuery("SELECT p FROM Patient p WHERE p.isDeleted ='FALSE' AND p.trackCode=?1");
        q.setParameter(1, trackCode);
        return (Patient) q.getSingleResult();
    }

    /**
     * Create new patient with their fullname, date of birth, job, address phone
     * @param fullName
     * @param dateOfBirth
     * @param job
     * @param address
     * @param phone
     * @return
     */
    //@WebMethod
    public String createPatient(String fullName, Date dateOfBirth, String job, String address, String phone) {
        Patient p = new Patient(fullName, dateOfBirth, job, address, phone);
        em.persist(p);
        return p.getTrackCode();
    }

    //@WebMethod
    public void edit(Patient patient) {
        em.merge(patient);
    }

    //@WebMethod
    public void removePatient(Long patientId) {
        Patient p = em.find(Patient.class, patientId);
        if (p == null) {
            throw new EntityNotFoundException("The patient with id " + patientId + " does not exist");
        } else {
            p.setIsDeleted(true);
            em.merge(p);
        }
    }

    //@WebMethod
    public void removePatients(List<Long> patientIds) {
        for (Long patientId : patientIds) {
            removePatient(patientId);
        }
    }

    //@WebMethod
    public void recover(String patientId) {
        Patient p = em.find(Patient.class, patientId);
        if (p == null) {
            throw new EntityNotFoundException("The vehicle with id " + p + " does not exist");
        }
        p.setIsDeleted(false);
        em.merge(p);
    }

    //@WebMethod
    public void removeForever(String patientId) {
        Patient p = em.find(Patient.class, patientId);
        if (p != null) {
            em.remove(p);
        }
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
